package com.qf.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.qf.utils.ConnectionUtils;
import jdk.nashorn.internal.objects.annotations.Property;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 * @Description: 公用常量
 * @Data: 2022/9/1 13:43
 * @Pacakge: com.qf.config
 * @ClassName: JdbcConfig
 * @Author: fan
 */
@Configuration
@ComponentScan(basePackages = "com.qf")
@PropertySource(value="classpath:jdbc.properties")
public class JdbcConfig {
    @Value("${driverClassName}")
    private String driverClassName;
    @Value("${jdbcUrl}")
    private String jdbcUrl;
    @Value("${user}")
    private String user;
    @Value("${password}")
    private String password;
    @Bean("queryRunner")
    public QueryRunner getQueryRunner(){
        return new QueryRunner(dataSource());
    }
    @Bean("dataSource")
    public DataSource dataSource() {
        DataSource dataSource = new ComboPooledDataSource();
        try {
            dataSource = new ComboPooledDataSource();
            ((ComboPooledDataSource)dataSource).setDriverClass(driverClassName);
            ((ComboPooledDataSource)dataSource).setJdbcUrl(jdbcUrl);
            ((ComboPooledDataSource)dataSource).setUser(user);
            ((ComboPooledDataSource)dataSource).setPassword(password);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        return dataSource;
    }
}

